<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Product extends CI_Controller {
    
    function __construct() {
        parent::__construct();
    }

    public function add_product(){}

    public function del_product(){}

    public function set_product(){
        $product_id = $this->input->get('product_id');
        if(!$product_id){
            api_failed('empty_product_id');
        }
        $sets = $this->input->post(null, true);
        if (empty($sets)) {
            api_failed('empty_sets');
        }
        $this->load->model('product_model');
        api_success(array(
            'result'=>$this->product_model->set_product($product_id, $sets)
        ));
    }

    public function set_products_order(){
        $product_orders = $this->input->post('products_id_order');
        $this->load->model('product_model');
        for($i=0;$i<count($product_orders);$i++){
            $sql_set = $i + 1;
            $sql_where =  $product_orders[$i];
            $data = $this->product_model->set_product_orders($sql_set, $sql_where);
            if(!$data)break;
        }
        api_success($data);
    }

    public function get_product_list() {
        $this->load->model('product_model');
        $this->load->model('setting_model');
        $this->load->model('vote_model');
        $this->load->model('share_model');
        $products = $this->product_model->get_all_products();
        $campaign_start_time = $this->setting_model
            ->get_setting_val('campaign_start_time');
        $campaign_start_time = strtotime($campaign_start_time);
        foreach ($products as &$product) {
//            print_r(generate_product_time($campaign_start_time, $product['product_order']));
            $product_time = generate_product_time($campaign_start_time, $product['product_order']);
            $product['product_real_vote_count'] = $this->vote_model->get_sum_production_vote($product['product_id']);
            $product['product_real_share_count'] = $this->share_model->get_share_count_of_product($product['product_id']);
            if ($product_time['vote_start_time'] > time()) {
                $product['state'] = 'pre_vote';
                $product['time_left'] = $product_time['vote_start_time'] - time();
            } else if ($product_time['vote_end_time'] > time()) {
                $product['state'] = 'in_vote';
                $product['time_left'] = $product_time['vote_end_time'] - time();
            } else if ($product_time['sell_end_time'] > time()) {
                $product['state'] = 'in_sell';
                $product['time_left'] = $product_time['sell_end_time'] - time();
            } else {
                $product['state'] = 'end_sell';
                $product['time_left'] = 0;
            }
        }
//        print_r($products); die();
        api_success(array('product_list'=>$products));
    }

    
    public function get_product(){
            $product_id = $this->input->get('product_id');
            if(!$product_id){
                    api_failed('empty_product_id');
            }
            $this->load->model('product_model');
            $this->load->model('vote_model');
            $this->load->model('share_model');
            $product = $this->product_model->get_product($product_id, null);
            $product['product_real_vote_count'] = $this->vote_model->get_sum_production_vote($product['product_id']);
            $product['product_real_share_count'] = $this->share_model->get_share_count_of_product($product['product_id']);
            $data = array('product'=>$product);
            api_success($data);
    }
    
    public function set_product_high_discount_picture() {
        $product_id = $this->input->get('product_id', true);
        $upload_path = 'uploads/pictures/';
        $upload_config = array(
            'upload_path' => $upload_path,
            'allowed_types' => 'jpg|png|jpeg',
            'max_size' => '1048576',
            'max_width' => '298',
            'max_height' => '175',
            'overwrite' => false,
            'encrypt_name' => true
        );
        $this->load->library('upload', $upload_config);
        if (! $this->upload->do_upload('product_high_discount_picture')) {
            api_failed($this->upload->display_errors());
        }
        $upload_picture = $this->upload->data();
        /*
        [file_name] => 885baca583d51c9f0c2ed6938cbf5f78.jpg
        [file_type] => image/jpeg
        [file_path] => D:/Work/Profero/projects/ComfortClothworld/app/uploads/photo/
        [full_path] => D:/Work/Profero/projects/ComfortClothworld/app/uploads/photo/885baca583d51c9f0c2ed6938cbf5f78.jpg
        [raw_name] => 885baca583d51c9f0c2ed6938cbf5f78
        [orig_name] => test_family.jpg
        [client_name] => test_family.jpg
        [file_ext] => .jpg
        [file_size] => 113.21
        [is_image] => 1
        [image_width] => 760
        [image_height] => 500
        [image_type] => jpeg
        [image_size_str] => width="760" height="500"
        */
        $this->load->model('product_model');
        $this->product_model->set_product_high_discount_picture($product_id,
            $upload_path.$upload_picture['file_name']);
        api_success(array('picture'=>'/app/'.$upload_path.$upload_picture['file_name']));
    }
    
    public function set_product_low_discount_picture() {
        $product_id = $this->input->get('product_id', true);
        $upload_path = 'uploads/pictures/';
        $upload_config = array(
            'upload_path' => $upload_path,
            'allowed_types' => 'jpg|png|jpeg',
            'max_size' => '1048576',
            'max_width' => '298',
            'max_height' => '175',
            'overwrite' => false,
            'encrypt_name' => true
        );
        $this->load->library('upload', $upload_config);
        if (! $this->upload->do_upload('product_low_discount_picture')) {
            api_failed($this->upload->display_errors());
        }
        $upload_picture = $this->upload->data();
        $this->load->model('product_model');
        $this->product_model->set_product_low_discount_picture($product_id,
            $upload_path.$upload_picture['file_name']);
        api_success(array('picture'=>'/app/'.$upload_path.$upload_picture['file_name']));
    }
    
    public function set_product_sharing_picture() {
        $product_id = $this->input->get('product_id', true);
        $upload_path = 'uploads/pictures/';
        $upload_config = array(
            'upload_path' => $upload_path,
            'allowed_types' => 'jpg|png|jpeg',
            'max_size' => '1048576',
            'max_width' => '430',
            'max_height' => '250',
            'overwrite' => false,
            'encrypt_name' => true
        );
        $this->load->library('upload', $upload_config);
        if (! $this->upload->do_upload('product_sharing_picture')) {
            api_failed($this->upload->display_errors());
        }
        $upload_picture = $this->upload->data();
        $this->load->model('product_model');
        $this->product_model->set_product_sharing_picture($product_id,
            $upload_path.$upload_picture['file_name']);
        api_success(array('picture'=>'/app/'.$upload_path.$upload_picture['file_name']));
    }
    
    public function set_product_pre_vote_picture() {
        $product_id = $this->input->get('product_id', true);
        $upload_path = 'uploads/pictures/';
        $upload_config = array(
            'upload_path' => $upload_path,
            'allowed_types' => 'jpg|png|jpeg',
            'max_size' => '1048576',
            'max_width' => '298',
            'max_height' => '200',
            'overwrite' => false,
            'encrypt_name' => true
        );
        $this->load->library('upload', $upload_config);
        if (! $this->upload->do_upload('product_pre_vote_picture')) {
            api_failed($this->upload->display_errors());
        }
        $upload_picture = $this->upload->data();
        $this->load->model('product_model');
        $this->product_model->set_product_pre_vote_picture($product_id,
            $upload_path.$upload_picture['file_name']);
        api_success(array('picture'=>'/app/'.$upload_path.$upload_picture['file_name']));
    }
    
    public function restore_product_vote_count() {
        $product_id = $this->input->get('product_id', true);
        if (empty($product_id)) {
            api_failed('empty_product_id');
        }
        $this->load->model('product_model');
        $this->load->model('vote_model');
        $product_vote_count = $this->vote_model->get_sum_production_vote($product_id);
        $set = $this->product_model->set_product_vote_count($product_id, $product_vote_count);
        if (!$set) {
            api_failed('restore_product_vote_count_failed');
        }
        api_success(array('product_vote_count'=>$product_vote_count));
    }
    
    public function restore_product_share_count() {
        $product_id = $this->input->get('product_id', true);
        if (empty($product_id)) {
            api_failed('empty_product_id');
        }
        $this->load->model('product_model');
        $this->load->model('share_model');
        $product_share_count = $this->share_model->get_share_count_of_product($product_id);
        $set = $this->product_model->set_product_share_count($product_id, $product_share_count);
        if (!$set) {
            api_failed('restore_product_share_count_failed');
        }
        api_success(array('product_share_count'=>$product_share_count));
    }
}